package com.czg.designmode.state.common;

/**
 * 糖果机
 * 初始状态-（装载糖果）》待机状态-（投入硬币）》准备状态-（转动曲柄）》售出状态-（没有糖果）》初始状态
 * 准备状态-（硬币退出）》待机状态
 */
@SuppressWarnings({"ALL", "AlibabaAvoidCommentBehindStatement"})
public class CandyMachine {

    /**
     * 状态值：0初始状态，1待机状态，2准备状态，3售出状态
     */
    private final static int SoldOutState = 0;
    private final static int OnReadyState = 1;
    private final static int HasCoin = 2;
    private final static int SoldState = 3;

    /**
     * 变量,用于存放当前的状态值
     */
    private int state = SoldOutState;

    /**
     * 糖果的数目
     */
    private int count = 0;

    public CandyMachine(int count) {
        this.count = count;
        if (count > 0) {
            state = OnReadyState;
        }
    }

    /**
     * 投入硬币行为的时候，通过判断当前的状态来匹配所有的状态.
     */
    public void insertCoin() {
        switch (state) {
            case SoldOutState:
                System.out.println("you can't insert coin,the machine sold out!");
                break;
            case OnReadyState: // 只有在待机状态的时候,投入硬币行为正确,并将状态改变为准备状态
                state = HasCoin;
                System.out.println("you have inserted a coin,next,please turn crank!");
                break;
            case HasCoin:
                System.out.println("you can't insert another coin!");
                break;
            case SoldState:
                System.out.println("please wait!we are giving you a candy!");
                break;
        }

    }

    /**
     * 回退硬币
     */
    public void returnCoin() {
        switch (state) {
            case SoldOutState:
                System.out.println("you can't return,you haven't inserted a coin yet!");
                break;
            case OnReadyState:
                System.out.println("you haven't inserted a coin yet!");
                break;
            case HasCoin:
                System.out.println("coin return!");
                state = OnReadyState;
                break;
            case SoldState:
                System.out.println("sorry,you already have turned the crank!");
                break;
        }

    }

    /**
     * 转动曲柄
     */
    public void turnCrank() {
        switch (state) {
            case SoldOutState:
                System.out.println("you turned,but there are no candies!");
                break;
            case OnReadyState:
                System.out.println("you turned,but you haven't inserted a coin!");
                break;
            case HasCoin:
                System.out.println("crank turn...!");
                state = SoldState;
                dispense();
                break;
            case SoldState:
                System.out.println("we are giving you a candy,turning another get nothing,!");
                break;
        }

    }

    /**
     * 触发发放糖果行为
     */
    private void dispense() {
        count = count - 1;
        System.out.println("a candy rolling out!");
        if (count > 0) {
            state = OnReadyState;
        } else {
            System.out.println("Oo,out of candies");
            state = SoldOutState;
        }

    }

    public void printState() {
        switch (state) {
            case SoldOutState:
                System.out.println("***SoldOutState***");
                break;
            case OnReadyState:
                System.out.println("***OnReadyState***");
                break;
            case HasCoin:
                System.out.println("***HasCoin***");
                break;
            case SoldState:
                System.out.println("***SoldState***");
                break;
        }

    }
}